package main

import (
	"fmt"
	"log"
	"net/http"
	"strings"
)

// 编写一个网页服务器监听端口 9999，有如下处理函数：

// 当请求 http://localhost:9999/hello/Name 时，响应：hello Name（Name 需是一个合法的姓，比如 Chris 或者 Madeleine）
// 当请求 http://localhost:9999/shouthello/Name 时，响应：hello NAME

func sayHello(w http.ResponseWriter, req *http.Request) {
	surName := req.URL.Path[len("/hello/"):]
	if surName != "" {
		runes := []rune(surName)

		surName = strings.ToUpper(string(runes[:1])) + string(runes[1:])
	}
	fmt.Fprintf(w, "hello %s", surName)
}

func shoutHello(w http.ResponseWriter, req *http.Request) {
	name := req.URL.Path[len("/shouthello/"):]
	if name != "" {
		name = strings.ToUpper(name)
	}
	fmt.Fprintf(w, "hello %s", name)
}

func main() {
	http.HandleFunc("/hello/", sayHello)
	http.HandleFunc("/shouthello/", shoutHello)

	err := http.ListenAndServe(":9999", nil)
	if err != nil {
		log.Fatal("ListenAndServe: " + err.Error())
	}
}
